<?php
/***************************************************************************
   Coppermine 1.3.1 for CPG-Dragonfly™
  **************************************************************************
   Port Copyright (c) 2004-2005 CPG Dev Team
   http://dragonflycms.org/
  **************************************************************************
   v1.1 (c) by Grégory Demar http://coppermine.sf.net/
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
****************************************************************************/
if (!defined('CPG_NUKE')) { die("You can't access this file directly..."); }

require("modules/{$module_name}/include/load.inc");

if (!GALLERY_ADMIN_MODE && !USER_CAN_CREATE_ALBUMS) {
	cpg_die(_ERROR, ACCESS_DENIED);
}

function get_subcat_data($parent, $ident = '')
{
	global $db, $CONFIG, $CAT_LIST, $ALBUM_DATA;
	$parent = intval($parent);
	$result = $db->query("SELECT cid id, catname name FROM {$CONFIG['TABLE_CATEGORIES']} WHERE parent = {$parent} AND cid>1 ORDER BY pos");
	foreach ($result as $subcat) {
		$subcat['name'] = $ident.$subcat['name'];
		$subcat['selected'] = ($subcat['id']==$ALBUM_DATA['category']);
		$CAT_LIST[] = $subcat;
		get_subcat_data($subcat['id'], $ident . '- ');
	}
}

$aid = $_GET->uint('id') ?: $_POST->uint('album');
if (!$aid) {
	if (GALLERY_ADMIN_MODE) {
		$ALBUM_DATA = $db->uFetchAssoc("SELECT * FROM {$CONFIG['TABLE_ALBUMS']}");
	} else {
		$ALBUM_DATA = $db->uFetchAssoc("SELECT * FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = " . (FIRST_USER_CAT + USER_ID));
	}
	if (!$ALBUM_DATA) cpg_die(_ERROR, ERR_NO_ALB_TO_MODIFY);
} else {
	$ALBUM_DATA = $db->uFetchAssoc("SELECT * FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid={$aid}");
	if (!$ALBUM_DATA) cpg_die(_CRITICAL_ERROR, NON_EXIST_AP);
}

if (!GALLERY_ADMIN_MODE && $ALBUM_DATA['category'] != FIRST_USER_CAT + USER_ID) {
	cpg_die(_ERROR, PERM_DENIED);
}

if ('POST' === $_SERVER['REQUEST_METHOD'] && isset($_POST['update']))
{
	if (!USER_CAN_CREATE_ALBUMS && !GALLERY_ADMIN_MODE) { cpg_die(_ERROR, PERM_DENIED); }
	$title       = Fix_Quotes($_POST->txt('title'), true);
	$description = Fix_Quotes(html2bb($_POST->txt('description')));
	$aid         = intval($_POST->uint('aid'));
	$category    = intval($_POST->uint('category'));
	$visibility  = intval($_POST->uint('visibility'));
	$thumb       = intval($_POST->uint('thumb'));
	$uploads     = intval($_POST->bool('uploads'));
	$comments    = intval($_POST->bool('comments'));
	$votes       = intval($_POST->bool('votes'));

	check_words($title);
	check_words($description);

	if (GALLERY_ADMIN_MODE) {
		$db->exec("UPDATE {$CONFIG['TABLE_ALBUMS']} SET title='$title', description='$description', thumb={$thumb}, comments={$comments}, votes={$votes}, visibility={$visibility}, category={$category}, uploads={$uploads} WHERE aid={$aid}");
	} else {
		$category = FIRST_USER_CAT + USER_ID;
		if ($visibility != $category && $visibility != $USER_DATA['group_id']) $visibility = 0; // not in 1.2.0
		$db->exec("UPDATE {$CONFIG['TABLE_ALBUMS']} SET title='$title', description='$description', thumb={$thumb}, comments={$comments}, votes={$votes}, visibility={$visibility} WHERE aid={$aid} AND category={$category}");
	}
	URL::redirect(URL::index("&cat=$category"), 1);
}

pageheader(sprintf(UPD_ALB_N, $ALBUM_DATA['title']));

$OUT = Dragonfly::getKernel()->OUT;
$OUT->album = $ALBUM_DATA;

if (GALLERY_ADMIN_MODE) {
	$OUT->albums = $db->query("SELECT
		aid id,
		CASE username WHEN NOT NULL THEN CONCAT('(', username, ') ', title) ELSE CONCAT(' - ', title) END label,
		CASE aid WHEN {$ALBUM_DATA['aid']} THEN 1 ELSE 0 END selected
	FROM {$CONFIG['TABLE_ALBUMS']} a
	LEFT JOIN {$CONFIG['TABLE_USERS']} u ON user_id = (category - " . FIRST_USER_CAT.")
	ORDER BY title");
} else {
	$OUT->albums = $db->query("SELECT
		aid id,
		title label,
		CASE aid WHEN {$ALBUM_DATA['aid']} THEN 1 ELSE 0 END selected
	FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = '" . (FIRST_USER_CAT + USER_ID) . "'
	ORDER BY title");
}

$result = $db->query("SELECT pid, filepath, filename, url_prefix FROM {$CONFIG['TABLE_PICTURES']} WHERE aid={$ALBUM_DATA['aid']} AND approved=1 ORDER BY filename");
$current_thumb = $THEME_DIR . '/images/nopic.jpg';
$options = array(array(
	'value'=>0,
	'label'=>LAST_UPLOADED,
	'image'=>$THEME_DIR . '/images/nopic.jpg',
));
while ($picture = $result->fetch_assoc()) {
	$thumb_url = get_pic_url($picture, 'thumb');
	if ($picture['pid'] == $ALBUM_DATA['thumb']) { $current_thumb = $thumb_url; }
	$options[] = array(
		'value'=>$picture['pid'],
		'label'=>$picture['filename'],
		'image'=>$thumb_url,
		'selected'=>$picture['pid'] == $ALBUM_DATA['thumb'],
	);
}
$OUT->album_thumb  = $current_thumb;
$OUT->album_thumbs = $options;

if (!GALLERY_ADMIN_MODE || $ALBUM_DATA['category'] > FIRST_USER_CAT) {
	$CAT_LIST = array(array($ALBUM_DATA['category'], USER_GAL));
} else {
	$CAT_LIST = array(array('id'=>0, 'name'=>NO_CAT));
	get_subcat_data(0, '');
}
$OUT->album_categories = $CAT_LIST;

$options = array(array(
	'value'=>0,
	'label'=>PUBLIC_ALB
));
if (GALLERY_ADMIN_MODE) {
	$options[] = array(
		'value'=>FIRST_USER_CAT+USER_ID,
		'label'=>ME_ONLY,
		'selected'=>((FIRST_USER_CAT+USER_ID)==$ALBUM_DATA['visibility']),
	);
	if ($ALBUM_DATA['category'] > FIRST_USER_CAT) {
		$result = $db->query("SELECT username FROM {$CONFIG['TABLE_USERS']} WHERE user_id=".intval($ALBUM_DATA['category'] - FIRST_USER_CAT));
		if ($result->num_rows) {
			$user = $result->fetch_row();
			$options[] = array(
				'value'=>$ALBUM_DATA['category'],
				'label'=>sprintf(OWNER_ONLY, $user[0]),
				'selected'=>($ALBUM_DATA['category']==$ALBUM_DATA['visibility']),
			);
		}
	}
	$result = $db->query("SELECT group_id, group_name FROM {$CONFIG['TABLE_USERGROUPS']}");
	while ($group = $result->fetch_row()) {
		$options[] = array(
			'value'=>$group[0],
			'label'=>sprintf(GROUPP_ONLY, $group[1]),
			'selected'=>($group[0]==$ALBUM_DATA['visibility']),
		);
	}
} else
if ($CONFIG['allow_private_albums'])
{
	$options[] = array(
		'value'=>FIRST_USER_CAT+USER_ID,
		'label'=>ME_ONLY,
		'selected'=>((FIRST_USER_CAT+USER_ID)==$ALBUM_DATA['visibility']),
	);
	$options[] = array(
		'value'=>$USER_DATA['group_id'],
		'label'=>$USER_DATA['group_name'],
		'selected'=>($USER_DATA['group_id']==$ALBUM_DATA['visibility']),
	);
}
$OUT->visibility_options = $options;

$OUT->display('coppermine/modifyalb');

pagefooter();
